<template>
  <div id="center">
    <div class="up" v-if="alive">
      <div class="bg-color-black item"  v-for="item in titleItem" :key="item.title" >
        <p class="ml-3 colorBlue fw-b">{{item.title}}</p>
        <div>
          <dv-digital-flop :config="item.number" style="width:1.25rem;height:.625rem;" />
        </div>
      </div>
    </div>
    <div class="down">
      <div class="percent">
        <div class="item bg-color-black">
          <span style="float:left">在线设备数量</span>
          <CenterChart :id="rate[0].id" :tips="rate[0].tips" :colorObj="rate[0].colorData" />
        </div>
        <div class="item bg-color-black" >
          <span style="float:left">离线设备数量</span>
          <CenterChart :id="rate[1].id" :tips="rate[1].tips" :colorObj="rate[1].colorData" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import CenterChart from "@/components/echart/center/centerChartRate";

export default {
  data () {
    return {
      alive: true,
      titleItem: [
        {
          title: "二氧化碳浓度",
          number: {
            number: [120],
            toFixed: 1,
            content: "{nt}"
          }
        },
        {
          title: "光照强度",
          number: {
            number: [18],
            toFixed: 1,
            content: "{nt}"
          }
        },
        {
          title: "空气湿度",
          number: {
            number: [2],
            toFixed: 1,
            content: "{nt}"
          }
        },
        {
          title: "空气温度",
          number: {
            number: [14],
            toFixed: 1,
            content: "{nt}"
          }
        },
        {
          title: "土壤温度",
          number: {
            number: [26],
            toFixed: 1,
            content: "{nt}"
          }
        },
        {
          title: "土壤湿度",
          number: {
            number: [100],
            toFixed: 1,
            content: "{nt}"
          }
        }
      ],
      
      // 通过率和达标率的组件复用数据
      rate: [
        {
          id: "centerRate1",
          tips: 2,
          colorData: {
            textStyle: "#3fc0fb",
            series: {
              color: ["#00bcd44a", "transparent"],
              dataColor: {
                normal: "#03a9f4",
                shadowColor: "#97e2f5"
              }
            }
          }
        },
        {
          id: "centerRate2",
          tips: 4,
          colorData: {
            textStyle: "#67e0e3",
            series: {
              color: ["#faf3a378", "transparent"],
              dataColor: {
                normal: "#ff9800",
                shadowColor: "#fcebad"
              }
            }
          }
        }
      ]
    };
  },
  components: {
    CenterChart
  },
  mounted(){
    this.init2() 
    this.init() 
  },
  methods:{
     init() {

        console.log("ws://localhost:8080"+"/ws/group")

        this.ws1 = new WebSocket("ws://localhost:8080"+"/ws/group/1")
                
        //监听是否连接成功
        this.ws1.onopen = ()=> {
             console.log('ws连接状态：' +this.ws1.readyState);
             //连接成功则发送一个数据
             this.ws1.send('连接成功');
          }
          
        //接听服务器发回的信息并处理展示
        this.ws1.onmessage = (message)=> {  
             let mes=JSON.parse(message.data)
             console.log("222",mes)
             if(mes.hasOwnProperty("co2")){
               this.titleItem[0].number.number.splice(1,1,mes.co2) //二氧化碳浓度
             }
             if(mes.hasOwnProperty("illumination")){
               this.titleItem[1].number.number[0]=mes.illumination //光照强度
             }
             if(mes.hasOwnProperty("humidity")){
               this.titleItem[2].number.number[0]=mes.humidity//空气湿度
             }
             if(mes.hasOwnProperty("temperature")){
               this.titleItem[3].number.number.splice(0,1,mes.temperature)  //空气温度
             }
             if(mes.hasOwnProperty("humidity1")){
               this.titleItem[4].number.number[0]=mes.humidity1//土壤湿度
             }
             
            if(mes.hasOwnProperty("temperature1")){
               this.titleItem[5].number.number[0]=mes.temperature1//土壤温度
             }
             console.log("111111",this.titleItem)
             this.reload()
             
         }
         
         //监听连接关闭事件
         this.ws1.onclose = ()=>{
            //监听整个过程中websocket的状态
            console.log('ws连接状态：' + this.ws1.readyState);
         }
         
         //监听并处理error事件
         this.ws1.onerror = function(error) {
            console.log(error);
         }
        },
    reload() {
        this.alive= false
        this.$nextTick(() => {
          this.alive = true
        })
      },
    init2() {

        console.log("ws://localhost:8080"+"/ws/deviceOnLine")

        this.ws = new WebSocket("ws://localhost:8080"+"/ws/deviceOnLine")
                
        //监听是否连接成功
        this.ws.onopen = ()=> {
             console.log('ws连接状态：' +this.ws.readyState);
             //连接成功则发送一个数据
             this.ws.send('连接成功');
          }
          
        //接听服务器发回的信息并处理展示
        this.ws.onmessage = (message)=> {  
             let mes=JSON.parse(message.data)
             this.rate[0].tips=mes.online 
             this.rate[1].tips=mes.offline 
         }
         
         //监听连接关闭事件
         this.ws.onclose = ()=>{
            //监听整个过程中websocket的状态
            console.log('ws连接状态：' + this.ws.readyState);
         }
         
         //监听并处理error事件
         this.ws.onerror = function(error) {
            console.log(error);
         }
        },
        beforeDestroy: function () {
        // 页面离开时断开连接,清除定时器
        this.disconnect();
        },
        disconnect(){
          this.ws1.close()
          this.ws.close()
        }
  }

};
</script>

<style lang="scss" scoped>
#center {
  display: flex;
  flex-direction: column;
  .up {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    .item {
      border-radius: 0.0625rem;
      padding-top: 0.2rem;
      margin-top: 0.1rem;
      width: 32%;
      height: 0.875rem;
    }
  }
  .down {
    padding: 0.07rem 0.05rem;
    padding-bottom: 0;
    width: 100%;
    display: flex;
    height: 3.1875rem;
    justify-content: space-between;
    .bg-color-black {
      border-radius: 0.0625rem;
    }
    .ranking {
      padding: 0.125rem;
      width: 59%;
    }
    .percent {
      width: 8.25rem;
      display: flex;
      flex-wrap: wrap;
      .item {
        width: 50%;
        height: 3.25rem;
        span {
          margin-top: 0.0875rem;
          display: flex;
          justify-content: center;
        }
      }
      .water {
        width: 100%;
      }
    }
  }
}

</style>